/**********************************
* 3、编写算法，用两个大小相同的栈
* 来模拟一个队列的操作
* (包括入队操作和出队操作两个算法) 。
***********************************/
#include<stdio.h>
#include<malloc.h>

typedef struct {int* data;int capacity,top; } stack;


stack* stack_new(int capacity){
    stack* s = malloc(sizeof(stack));
    s->capacity = capacity;
    s->top = 0;
    s->data = malloc(sizeof(int)*capacity);
    return s;
}

void stack_del(stack* s){free(s->data);free(s);}

int stack_pop(stack* s){
    if(s->top==0){perror("Empty stack");return 0;}
    return s->data[--s->top];
}

void stack_push(stack* s,int data){if(s->top < s->capacity)s->data[s->top++] = data;}

int main(){
    stack* a = stack_new(1024);
    stack* b = stack_new(1024);
    int data[100],i,j,k;
    for(i=0;i<100;i++)data[i]=i;
    // 100个数据入队
    for(i=0;i<100;i++)stack_push(a,data[i]);
    // 出队10个数据
    while(a->top)stack_push(b,stack_pop(a));
    for(i=0;i<10;i++)printf("%d ",stack_pop(b));

    printf("\nstack b has %d elements left.\n",b->top);

    stack_del(a); stack_del(b);
    return 0;

}